//
//  XHzhangShangViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-25.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHzhangShangViewController.h"
#import "XHTableViewHeaderView.h"
#import "XHZhangShangViewModel.h"
#import <MBProgressHUD.h>
#import "MainEnterViewController.h"
#import "dangeViewController.h"
#import  "guangfangfabuViewController.h"
#define LIST @[@"政务资讯",@"官方发布"]

@interface XHzhangShangViewController ()
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) XHZhangShangViewModel *viewModel;
@property (nonatomic ,strong) MBProgressHUD *HUD;
@end



@implementation XHzhangShangViewController

@synthesize titleLable = _titleLable;
@synthesize navBar= _navBar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"ZhangShangView" owner:self options:nil];
        UIView *view = [array objectAtIndex:0];
        CGRect rect = self.view.frame;
        CGFloat oY = kIOS_VERSION<7.0?44:64;
        self.titleLable.text = @"掌上政务";
        [view setFrame:CGRectMake(0, oY, rect.size.width, rect.size.height-oY)];
        [self.view addSubview:view];
        
         CGFloat aY = kIOS_VERSION<7.0?0:20;
        _navBar = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0+aY)];
        _navBar.backgroundColor = [UIColor colorWithRed:2.0/255 green:102.0/255 blue:172.0/255 alpha:1.0];
        _navBar.userInteractionEnabled = YES;
        
        _titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, aY, self.view.frame.size.width, 44)];
        _titleLable.backgroundColor = [UIColor clearColor];
        _titleLable.text = @"掌上政务"  ;
        _titleLable.textColor = [UIColor whiteColor];
        _titleLable.font = [UIFont systemFontOfSize:20];
        _titleLable.textAlignment = NSTextAlignmentCenter;
        [_navBar addSubview:_titleLable];
        [self.view addSubview:_navBar];
        UIButton* _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, aY, 40,40.0)];
        [_leftButton setImage:[UIImage imageNamed:@"icon_back@2x.png"] forState:UIControlStateNormal];
        _leftButton.imageEdgeInsets = UIEdgeInsetsMake(10, 13, 5, 5);
        [_leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_leftButton];
           @weakify(self);
        self.rightButton.hidden = YES;
        self.viewModel = [[XHZhangShangViewModel alloc]  init:nil huiming:nil];
        NSUserDefaults* defa = [NSUserDefaults standardUserDefaults];
       
       
       
        self.viewModel.titleArray = [defa objectForKey:@"政务栏目"];
        self.viewModel.tempdataArray = [defa objectForKey:@"政务新闻"];
        NSLog(@"缓存政务新闻 =%@",self.viewModel.tempdataArray);

        self.tableView = [[UITableView alloc] init];
        self.tableView.frame = CGRectMake(10, oY+5, 300, self.view.frame.size.height-oY-5);
        self.tableView.dataSource = self.viewModel;
        self.tableView.delegate = self;
        self.tableView.bounces = NO;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
        
        
        [RACObserve(self.viewModel, hasLoad) subscribeNext:^(id x) {
            if ([x intValue]) {
                @strongify(self);
                [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:YES];
            }
        }];
        self.HUD = [[MBProgressHUD alloc] initWithView:view];
        [self.view addSubview:self.HUD];
        
        self.HUD.labelText = @"正在加载";
        self.HUD.dimBackground = NO;
        [self.HUD show:YES];
        if ([self.viewModel.titleArray count] > 0)
        {
            [self refresh];
        }

    }
    return self;
}

-(void)back
{
     // [[AFHTTPRequestOperationManager manager].operationQueue cancelAllOperations];
   // [self.viewModel canall];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)refresh
{   NSLog(@"refesh");
    [self.tableView reloadData];
    [self.HUD hide:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString* string = [[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    NSLog(@"string = %@",string);
      if ([string isEqualToString:@"官方发布"])
    {
        guangfangfabuViewController* vie = [[guangfangfabuViewController alloc] init];
        vie.neewId =  [[[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"id"] stringValue];
        [self.navigationController pushViewController:vie animated:YES];
        NSLog(@"dic = %@",[self.viewModel.titleArray objectAtIndex:indexPath.row]);
        NSLog(@"id = %@",[[[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"id"] stringValue]);
        butoncl* bl = [[butoncl alloc] init];
        //NSString* str = [viewControoler.yinitData[@"id"] stringValue];
        [bl postString:@"001" fff:[[[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"id"] stringValue]];
    }
    else
    {
        dangeViewController* viewContr = [[dangeViewController alloc] init];
        viewContr.newdId = [[[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"id"] stringValue];
        NSLog(@"id = %@",viewContr.newdId);
        viewContr.titleString = string;
        [self.navigationController pushViewController:viewContr animated:YES];butoncl* bl = [[butoncl alloc] init];
        //NSString* str = [viewControoler.yinitData[@"id"] stringValue];
        [bl postString:@"001" fff:viewContr.newdId];
    }

}

@end
